﻿using PermissionDesign.Model;
using PermissionDesign.Model.Entity;
using PermissionDesign.Model.Request;
using PermissionDesign.Model.Response;
using PermissionDesign.Tool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace PermissionDesign.Service
{
    /// <summary>
    /// 用户服务
    /// </summary>
    public class UserService : BaseService
    {
        public UserService(DataContext dbContext) : base(dbContext)
        {
            _dbContext = dbContext;
        }

        #region 分页查询用户信息
        /// <summary>
        /// 分页查询用户信息
        /// </summary>
        /// <param name="where"></param>
        /// <param name="total"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public ResponseModel UserPageQuery(List<Expression<Func<User, bool>>> where, out int total, int pageIndex = 1, int pageSize = 10)
        {
            //查询出除超级管理员外的其他用户
            var list = _dbContext.User.Where(c => c.Role.RoleEncoding != EnumHelper.SingleTon().GetEnumDescription(EnumConfig.SuperAdminEncoding));
            foreach (var item in where)
            {
                list = list.Where(item);
            }
            total = list.Count();
            var pageData = list.OrderByDescending(c => c.Id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

            var response = new ResponseModel();
            response.code = 200;
            response.data = new List<Response_User>();
            foreach (var user in pageData)
            {
                response.data.Add(new Response_User
                {
                    Id = user.Id,
                    RoleId = user.Role.Id,
                    RoleName = user.Role.RoleName,
                    LoginAccount = user.LoginAccount,
                    Mobile = user.Mobile,
                    Name = user.Name,
                    Sex = user.Sex,
                    SexName = user.Sex == 1 ? "男" : "女",
                    Birth = user.Birth.ToString("yyyy-MM-dd"),
                    IsEnabled = user.IsEnabled,
                    EnableName = user.IsEnabled == 1 ? "启用" : "禁用",
                    RegisterTime = user.RegisterTime.ToString("yyyy-MM-dd HH:mm:ss"),
                    UpdateTime = user.UpdateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                    Remark = user.Remark
                });
            }
            return response;
        }
        #endregion

        #region 查询用户是否存在
        /// <summary>
        /// 查询用户是否存在
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public bool IsUserExist(Expression<Func<User, bool>> where)
        {
            return _dbContext.User.FirstOrDefault(where) != null;
        }
        #endregion

        #region 添加用户
        public ResponseModel AddUser(Request_AddUser model)
        {
            var key = CodeHelper.SingleTon().GetRandomCode(6);
            var user = new User
            {
                RoleId = model.RoleId,
                LoginKey = key,
                LoginAccount = model.LoginAccount,
                LoginPassword = PassHelper.SingleTon().EncryptByMd5(key + model.LoginPassword),
                Name = model.Name,
                Mobile = model.Mobile,
                Sex = model.Sex,
                Birth = model.Birth,
                IsEnabled = 1,
                RegisterTime = DateTime.Now,
                UpdateTime = DateTime.Now,
                Remark = model.Remark
            };
            _dbContext.User.Add(user);
            var i = _dbContext.SaveChanges();
            if (i > 0)
            {
                return new ResponseModel { code = 200, result = "添加用户成功" };
            }
            else
            {
                return new ResponseModel { code = 0, result = "添加用户失败" };
            }
        }

        #endregion

        #region 编辑用户
        /// <summary>
        /// 编辑用户
        /// </summary>
        /// <param name="editUser"></param>
        /// <returns></returns>
        public ResponseModel EditUser(Request_EditUser editUser)
        {
            if (!this.IsUserExist(c => c.Id == editUser.editId))
            {
                return new ResponseModel { code = 0, result = "编辑的用户不存在" };
            }

            var user = _dbContext.User.Find(editUser.editId);//获取到编辑的用户信息
            user.RoleId = editUser.editRoleId;
            user.Mobile = editUser.editMobile;
            user.Name = editUser.editName;
            user.Sex = editUser.editSex;
            user.Birth = editUser.editBirth;
            user.IsEnabled = editUser.editIsEnabled;
            user.Remark = editUser.editRemark;
            user.UpdateTime = DateTime.Now;
            _dbContext.User.Update(user);
            var i = _dbContext.SaveChanges();
            if (i > 0)
            {
                return new ResponseModel { code = 200, result = "编辑用户成功" };
            }
            else
            {
                return new ResponseModel { code = 0, result = "编辑用户失败" };
            }
        }
        #endregion

    }
}
